var scrollTypeLEFT = "SCROLL_LEFT";	// Scroll qua huong ben trai
var scrollTypeRIGHT= "SCROLL_RIGHT";// Scroll qua huong ben phai
var scrollTypeUP = "SCROLL_UP";		// Scroll huong len tren
var scrollTypeDOWN= "SCROLL_DOWN";	// Scroll huong xuong duoi
var scrolling=false;				// Thong bao la noi dung dang duoc scroll
var limitScrollWidth = 50;			// Gioi han muc toi da khi scroll content
var limitEachScrollWidth = 1;		
var extraWidth = 10;
var extraHeight = 5;

// Ham nay duoc dung khi Content duoc auto scroll, nghia la chi can dua chuot vao la tu dong scroll.
function autoScroll(scrollName, scrollType, scrollWidth, delay, width, height, isLooped)
{
	if (!scrolling)
	{
		scrolling = true;
		
		var scrollVal = 0;
		if (scrollType==scrollTypeUP || scrollType==scrollTypeLEFT) scrollVal = -1;
		else if (scrollType==scrollTypeDOWN || scrollType==scrollTypeRIGHT) scrollVal = 1;
		
		if (scrollType==scrollTypeUP || scrollType==scrollTypeDOWN)	// Scroll len hoac xuong
		{
			scrollVertical(scrollName, scrollWidth, scrollVal, delay, width, height, isLooped);
		}
		if (scrollType==scrollTypeLEFT || scrollType==scrollTypeRIGHT)	// Scroll qua trai hoac qua phai
		{
			scrollHorizontal(scrollName, scrollWidth, scrollVal, delay, width, height, isLooped);
		}
	}
}
// Ham nay duoc dung khi Content duoc scroll khi click vao bieu tuong scroll
function clickScroll(scrollName, scrollType, scrollWidth, delay, width, height, isLooped)
{
	var scrollVal = 0;
	
	if (isLooped)
	{
		scrollToRightPosition(scrollName, scrollType, width, height);
	}

	if (scrollType==scrollTypeUP || scrollType==scrollTypeLEFT) scrollVal = -1;
	else if (scrollType==scrollTypeDOWN || scrollType==scrollTypeRIGHT) scrollVal = 1;
	
	if (scrollType==scrollTypeUP || scrollType==scrollTypeDOWN)	// Scroll len hoac xuong
	{
		clickScrollVertical(scrollName, 0, scrollVal, delay, width, height, isLooped);
	}
	if (scrollType==scrollTypeLEFT || scrollType==scrollTypeRIGHT)	// Scroll qua trai hoac phai
	{
		clickScrollHorizontal(scrollName, 0, scrollVal, delay, width, height, isLooped);
	}
}
// Ham nay duoc dung khi goi Loop scroll, dua ve vi tri dung sau khi scroll het duong bien.
function scrollToRightPosition(scrollName, scrollType, width, height)
{
	scrollObj = document.getElementById(scrollName);
	if (scrollType==scrollTypeRIGHT)
	{
		//console.log((scrollObj.scrollLeft+width+extraWidth)+">" + (scrollObj.scrollWidth-(width/2)));
		if (scrollObj.scrollLeft+width+extraWidth>scrollObj.scrollWidth-(width/2))
		{
			scrollObj.scrollLeft = 0;
		}
	}
	if (scrollType==scrollTypeLEFT)
	{
		if (scrollObj.scrollLeft<=(width/2))
		{
			scrollObj.scrollLeft = scrollObj.scrollWidth - width - extraWidth;
		}
	}
	if (scrollType==scrollTypeUP)
	{
		if (scrollObj.scrollTop<=(height/2))
		{
			scrollObj.scrollTop = scrollObj.scrollHeight - height - extraHeight ;
		}
	}
	if (scrollType==scrollTypeDOWN)
	{
		if ((scrollObj.scrollTop + height + extraHeight)>=scrollObj.scrollHeight-(height/2))
		{
			scrollObj.scrollTop = 0;
		}
	}
}
// Ham nay duoc goi khi doi tuong duoc scroll len hay xuong bang cach click chuot.
function clickScrollVertical(scrollName, scrollWidth, scrollVal, delay, width, height, isLooped)
{
	scrollObj = document.getElementById(scrollName);
	skipWidth = 30;
	if (scrollWidth<=height-skipWidth) {
		scrollObj.scrollTop = scrollObj.scrollTop + (skipWidth)*scrollVal;
		scrollWidth += skipWidth;
		self.setTimeout("clickScrollVertical('"+scrollName+"'," + scrollWidth + ","+scrollVal+","+delay+","+width+","+height+","+isLooped+")", delay);
	} else
	{
		nextScroll = height - scrollWidth + extraHeight + 1;
		scrollObj.scrollTop = scrollObj.scrollTop + (nextScroll)*scrollVal;
	}
	activeScrollButtons(scrollName, true, width, height, isLooped);
}
// Ham nay duoc goi khi doi tuong scroll len hoac xuong khi dua chuot vao doi tuong scroll.
function scrollVertical(scrollName, scrollWidth, scrollVal, delay, width, height, isLooped)
{
	if (scrollWidth>limitScrollWidth) scrollWidth = limitScrollWidth;
	if (scrolling) {
		scrollObj = document.getElementById(scrollName);
		scrollObj.scrollTop = scrollObj.scrollTop + scrollWidth*(scrollVal);
		if (isLooped && (scrollObj.scrollTop==0 || scrollObj.scrollTop+height+extraHeight>=scrollObj.scrollHeight))
		{
			scrollToRightPosition(scrollName, (scrollVal<0?scrollTypeUP:scrollTypeDOWN), width, height);
		}

		scrollWidth += limitEachScrollWidth;
		activeScrollButtons(scrollName, true, width, height, isLooped);
		self.setTimeout("scrollVertical('"+scrollName+"'," + scrollWidth + ","+scrollVal+","+delay+","+width+","+height+","+isLooped+")", delay);
	}
}
// Ham nay duoc goi khi doi tuong scroll qua trai hay phai khi dua chuot vao doi tuong scroll.
function scrollHorizontal(scrollName, scrollWidth, scrollVal, delay, width, height, isLooped)
{
	//console.log(scrollName + "," + scrollWidth + "," +  scrollVal + "," + delay);
	if (scrollWidth>limitScrollWidth) scrollWidth = limitScrollWidth;
	if (scrolling) {
		scrollObj = document.getElementById(scrollName);
		scrollObj.scrollLeft = scrollObj.scrollLeft + scrollWidth*(scrollVal);
		if (isLooped && (scrollObj.scrollLeft==0 || scrollObj.scrollLeft+width+extraWidth>=scrollObj.scrollWidth))
		{
			scrollToRightPosition(scrollName, (scrollVal<0?scrollTypeLEFT:scrollTypeRIGHT), width, height);
		}
		scrollWidth += limitEachScrollWidth;
		activeScrollButtons(scrollName, false, width, height, isLooped);
		self.setTimeout("scrollHorizontal('"+scrollName+"'," + scrollWidth + ","+scrollVal+","+delay+","+width+","+height+","+isLooped+")", delay);
	}
}
// Ham nay duoc goi khi doi tuong scroll qua trai hoac phai khi click chuot vao doi tuong scroll..
function clickScrollHorizontal(scrollName, scrollWidth, scrollVal, delay, width, height, isLooped)
{
	scrollObj = document.getElementById(scrollName);
	skipWidth = 30;
	if (scrollWidth<=width-skipWidth) {
		scrollObj.scrollLeft = scrollObj.scrollLeft + (skipWidth)*scrollVal;
		scrollWidth += skipWidth;
		self.setTimeout("clickScrollHorizontal('"+scrollName+"'," + scrollWidth + ","+scrollVal+","+delay+","+width+","+height+","+isLooped+")", delay);
	} else
	{
		nextScroll = width - scrollWidth + extraWidth + 1;
		scrollObj.scrollLeft = scrollObj.scrollLeft + (nextScroll)*scrollVal;
	}
	activeScrollButtons(scrollName, false, width, height, isLooped);
}
// Ham nay xay dung noi dung cua doi tuong la Scroll len hay xuong.
function verticalContent(scrollName, content, width, height, delay, isClicked, isLooped)
{
	var act = "onmouseover=\"autoScroll('"+scrollName+"', scrollTypeUP, 1, "+delay+", "+width+", "+height+","+isLooped+");\"";
	var act2= "onmouseover=\"autoScroll('"+scrollName+"', scrollTypeDOWN, 1, "+delay+", "+width+", "+height+","+isLooped+");\"";
	if (isClicked) {
		act = "onClick=\"clickScroll('"+scrollName+"', scrollTypeUP, 1, "+delay+", "+width+", "+height+","+isLooped+");\"";
		act2= "onClick=\"clickScroll('"+scrollName+"', scrollTypeDOWN, 1, "+delay+", "+width+", "+height+","+isLooped+");\"";
	}
	//alert(act);
	strContent = "<table border=\"0\" height="+(height+extraHeight)+" width="+(width+extraWidth)+" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">"+
					"	<tr>"+
					"		<td height=10 width="+(width+extraWidth)+" align=\"center\" valign=top>"+
					"			<img src=\"./images/scrollup.gif\" width=69 height=7 id="+scrollName+"_imgScrollUp border=\"0\" onmouseout=\"scrolling=false;\" "+
					"			"+act+"/>"+
					"		</td>"+
					"	</tr>"+
					"	<tr>"+
					"		<td align=center width=\""+(width+extraWidth)+"\">"+
					"			<div id=\""+scrollName+"\"  style=\"height:"+(height+extraHeight)+"; width:"+(width+extraWidth)+"px; overflow-x:hidden; overflow-y: hidden; "+
					"			padding-top:0px; padding-bottom:0px\">"+
					"				" + content + "    "+
					"			</div>"+
					"		</td>"+
					"	</tr>"+
					"	<tr>"+
					"		<td height=10 width="+(width+extraWidth)+" align=\"center\" valign=bottom>"+
					"			<img src=\"./images/scrolldown.gif\" width=69 height=7 id="+scrollName+"_imgScrollDown border=\"0\" onmouseout=\"scrolling=false;\" "+
					"			"+act2+" />"+
					"		</td>"+
					"	</tr>"+
					"</table>";
	//alert(strContent);				
	return strContent;
}
// Tao doi tuong scroll len hay xuong
function makeVerticalScrollable(scrollObj, scrollName, width, height, delay, isClicked, isLooped)
{
	 var scrollObj = $(scrollObj);
	 content = scrollObj[0].innerHTML;
	 content = verticalContent(scrollName, content, width, height, delay, isClicked, isLooped);
	 scrollObj[0].innerHTML = content;
	 
	 activeScrollButtons(scrollName, true, width, height, isLooped);
}
// Ham nay tao noi dung doi tuong scroll qua trai hay phai
function horizontalContent(scrollName, content, width, height, delay, isClicked, isLooped)
{
	act = "onmouseover=\"autoScroll('"+scrollName+"', scrollTypeLEFT, 1, "+delay+", "+width+", "+height+", "+isLooped+");\"";
	act2= "onmouseover=\"autoScroll('"+scrollName+"', scrollTypeRIGHT, 1, "+delay+", "+width+", "+height+", "+isLooped+");\"";
	if (isClicked)
	{
		act = "onClick=\"clickScroll('"+scrollName+"', scrollTypeLEFT, 1, "+delay+", "+width+", "+height+", "+isLooped+");\"";
		act2= "onClick=\"clickScroll('"+scrollName+"', scrollTypeRIGHT, 1, "+delay+", "+width+", "+height+", "+isLooped+");\"";
	}
	strContent = "<table border=\"0\" height="+(height+extraHeight)+" width="+(width+extraWidth)+" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">" + 
					"<tr valign=middle>" + 
						"<td height="+(height+extraHeight)+" width=10 align=\"left\" valign=\"middle\" nowrap>" + 
							"<img src=\"./images/scrollleft.gif\" width=7 height=69 id="+scrollName+"_imgScrollLeft border=\"0\" onmouseout=\"scrolling=false;\" "+act+"/>" + 
						"</td>" + 
						"<td height="+(height+extraHeight)+" width=\""+(width+extraWidth)+"\" valign=middle>" + 
							"<pre id=\""+scrollName+"\" style=\"height:"+(height+extraHeight+15)+"px; width:"+(width+extraWidth)+"px; overflow-x:hidden; overflow-y: hidden; padding-left:0px; vertical-align:middle\">" + content + 
							"</pre>" + 
						 "</td>" + 
						"<td height="+(height+extraHeight)+" width=10 align=\"right\" valign=\"middle\" nowrap>" + 
							"<img src=\"./images/scrollright.gif\" width=7 height=69 id="+scrollName+"_imgScrollRight border=\"0\" onmouseout=\"scrolling=false;\" "+act2+"/>" + 
						"</td>" + 
					"</tr>" + 
				"</table>";
	return strContent;
}
// Tao doi tuong scroll qua trai hay phai
function makeHorizontalScrollable(scrollObj, scrollName, width, height, delay, isClicked, isLooped)
{
	 var scrollObj = $(scrollObj);
	 content = scrollObj[0].innerHTML;
	 content = horizontalContent(scrollName, content, width, height, delay, isClicked, isLooped);
	 scrollObj[0].innerHTML = content;
	 
	 activeScrollButtons(scrollName, false, width, height, isLooped);
}
// Show lai button scroll sau moi lan scroll.
function activeScrollButtons(scrollName, isVerticalScroll, width, height, isLooped)
{
	scrollObj = document.getElementById(scrollName);
	//if (!isVerticalScroll)
	//	console.log(scrollObj.scrollWidth + "<=" + (width+extraWidth));
	if (isVerticalScroll && scrollObj.scrollHeight<=height+extraHeight)
	{
		document.getElementById(scrollName + "_imgScrollUp").src = "./images/blank.gif";
		document.getElementById(scrollName + "_imgScrollDown").src = "./images/blank.gif";
		return;
	}
	else if (!isVerticalScroll && scrollObj.scrollWidth<=width+extraWidth)
	{
		document.getElementById(scrollName + "_imgScrollLeft").src = "./images/blank.gif";
		document.getElementById(scrollName + "_imgScrollRight").src = "./images/blank.gif";
		return;
	}
	if (!isLooped)
	{
		if (isVerticalScroll) // Active Vertical Scroll Buttons
		{
			
			if (scrollObj.scrollTop>0)
				document.getElementById(scrollName + "_imgScrollUp").src = "./images/scrollup.gif";
			else	
				document.getElementById(scrollName + "_imgScrollUp").src = "./images/scrollupDis.gif";
			
			if (scrollObj.scrollTop<scrollObj.scrollHeight-height-extraHeight) 
				document.getElementById(scrollName + "_imgScrollDown").src = "./images/scrolldown.gif";
			else	
				document.getElementById(scrollName + "_imgScrollDown").src = "./images/scrolldownDis.gif";	
			
			
		}
		else // Active Horizontal Scroll Buttons
		{
			if (scrollObj.scrollLeft>0)
				document.getElementById(scrollName + "_imgScrollLeft").src = "./images/scrollleft.gif";
			else	
				document.getElementById(scrollName + "_imgScrollLeft").src = "./images/scrollleftDis.gif";
			
			if (scrollObj.scrollLeft<scrollObj.scrollWidth-width-extraWidth)
				document.getElementById(scrollName + "_imgScrollRight").src = "./images/scrollright.gif";
			else	
				document.getElementById(scrollName + "_imgScrollRight").src = "./images/scrollrightDis.gif";	
		}
	}
}